<template>
	<view class="content">
		<view class="box"
			:style='{ "width": "100%", "padding": "0 40rpx", "alignItems": "center", "background": "url(http://codegen.caihongy.cn/20250208/663f8e6fd10444f08c8ffd7a9da52baf.png) no-repeat center top / 100% auto", "display": "flex", "height": "100%" }'>
			<view :style='{ "width": "100%", "padding": "0", "position": "relative", "background": "none", "height": "auto" }'>
				<view style="text-align: center; font-size: 36rpx; font-weight: bold; margin-bottom: 40rpx;">
					密码重置
				</view>
				<view
					:style='{ "border": "none", "width": "100%", "margin": "0 0 40rpx 0", "alignItems": "center", "display": "flex", "height": "auto" }'
					class="uni-form-item uni-column">
					<view
						:style='{ "padding": "0", "color": "#707070", "borderRadius": "0", "textAlign": "center", "background": "none", "flex": "none", "width": "160rpx", "lineHeight": "50rpx", "fontSize": "28rpx", "height": "50rpx" }'
						class="label">账号：</view>
					<input v-model="username"
						:style='{ "border": "none", "padding": "0px 24rpx", "margin": "0px", "borderColor": "#D6D6D6", "color": "rgb(0, 0, 0)", "borderRadius": "0", "flex": "1", "background": "none", "borderWidth": "0 0 2rpx 0 ", "fontSize": "28rpx", "borderStyle": "solid", "height": "80rpx" }'
						type="text" class="uni-input" name="" placeholder="请输入账号" />
				</view>
                
                <view class="tip-message" :style='{ "width": "100%", "margin": "20rpx 0 40rpx 0", "textAlign": "center", "color": "#707070", "fontSize": "28rpx" }'>
                    点击"重置密码"按钮后，密码将被重置为：123456
                </view>

				<button class="btn-submit" @tap="onResetTap" type="primary"
					:style='{ "border": "0", "padding": "0px", "margin": "0 0 24rpx 0", "color": "#fff", "borderRadius": "8rpx", "background": "linear-gradient( 135deg, #9EF0EA 0%, #99ABFF 100%)", "width": "100%", "lineHeight": "80rpx", "fontSize": "32rpx", "fontWeight": "600", "height": "80rpx" }'>重置密码</button>
				
				<view class="links"
					:style='{ "width": "100%", "margin": "0 0 24rpx 0", "flexWrap": "wrap", "display": "flex", "height": "auto" }'>
					<view class="link-highlight" @tap="onBackLogin"
						:style='{ "color": "#6B6153", "padding": "0 8rpx", "fontSize": "28rpx" }'>返回登录</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			username: '',
		}
	},
	onLoad() {
		// 如果有用户信息，自动填充用户名
		let userInfo = uni.getStorageSync('userSession');
		if (userInfo) {
			try {
				userInfo = JSON.parse(userInfo);
				if (userInfo.username) {
					this.username = userInfo.username;
				} else if (userInfo.zhanghao) {
					this.username = userInfo.zhanghao;
				} else if (userInfo.yuangongzhanghao) {
					this.username = userInfo.yuangongzhanghao;
				}
			} catch (e) {
				console.error(e);
			}
		}
	},
	methods: {
		onBackLogin() {
			uni.navigateTo({
				url: '../login/login'
			});
		},
		async onResetTap() {
			if (!this.username) {
				this.$utils.msg('请输入用户名')
				return
			}
			
			try {
				// 调用统一重置密码API
				const res = await this.$api.get('/user/resetPass', {
					username: this.username
				});
				
				if (res.code === 0) {
					this.$utils.msg(res.msg || '密码已重置为：123456');
					setTimeout(() => {
						uni.navigateTo({
							url: '../login/login'
						});
					}, 1500);
				} else {
					this.$utils.msg(res.msg || '密码重置失败，请检查账号是否存在');
				}
			} catch (e) {
				console.error(e);
				this.$utils.msg('密码重置失败，请检查网络连接');
			}
		}
	}
}
</script>

<style lang="scss" scoped>
page {
	height: 100%;
}

.content {
	height: 100%;
	box-sizing: border-box;
}
</style>
